import os
import httpx

DASHSCOPE_API_KEY = os.getenv('DASHSCOPE_API_KEY')
DASHSCOPE_API_URL = 'https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation'

PROMPT = '请判断以下内容是否包含违法、违规、涉黄、涉政、广告等不当信息，只回答"合规"或"不合规"：\n{}'

headers = {
    'Authorization': f'Bearer {DASHSCOPE_API_KEY}',
    'Content-Type': 'application/json'
}

def llm_check_text(text):
    payload = {
        "model": "qwen-turbo",
        "input": {
            "prompt": PROMPT.format(text)
        }
    }
    try:
        resp = httpx.post(DASHSCOPE_API_URL, headers=headers, json=payload, timeout=10)
        resp.raise_for_status()
        data = resp.json()
        # 通义千问返回结构可能有变化，需根据实际API文档调整
        result = data.get('output', {}).get('text', '')
        return '合规' in result and '不合规' not in result
    except Exception as e:
        # 检测失败时默认拦截
        return False 